package player;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import planes.plane;
import GAME.game;
import weaponrelated.*;

public abstract class player extends plane implements Runnable {
	
	protected boolean shoot;
	
	public void shoot(){
		this.arma.allyshoot();
	}
	public abstract void seticonoderecha();
	public abstract void seticononormal();
	public abstract void seticonoizquierda();
	public void activarmachinegun(){
		arma=new machinegun(game,this);
		
		
	}
	public void hit(int i){
		hp=hp-i;
		game.refreshhp();
	}
	public void activarinsanemachinegun(){
		arma=new insanemachinegun(game,this);
	}
	public void activarrocketlauncher(){
		arma=new rocketlauncher(game,this);
	}
	public void activardobledisparo(){
		arma=new doblemachinegun(game);
	}
	
	public void setup(boolean i){
		up=i;
	}
	public void setdown(boolean i){
		down=i;
	}
	public void setleft(boolean i){
		left=i;
	}
	public void setright(boolean i){
		right=i;
	}
	public void setshoot(boolean i){
		shoot=i;
	}
	public void kill(){
		
	
	hp=0;
	Thread aux=new animacionexpjugador(this);
	aux.start();
	aux=null;
	arma=null;
	
	
	
}
	public void mover(){
		Point pos = this.getLocation();
		
		if(up && !down && !left && !right){ //mov arriva
			if(pos.getY()>0)
			for(int i=1;i<=vel;i++){
				int v = pos.y - 1;
				pos.setLocation(pos.x, v);
				}
		}
		if(down && !up && !left && !right){ // mov abajo
			if(pos.getY()<(game.getHeight()-80))
			for(int i=1;i<=vel;i++){
			int v = pos.y + 1;
			pos.setLocation(pos.x, v);
			}
		}
		if(left && !up && !right && !down){ // mov izquierda
			if(pos.getX()>0){
			for(int i=1;i<=vel;i++){			
				int v = pos.x - 1;
				pos.setLocation(v, pos.y);
				}
		}
		}
		if(right && !up && !down && !left){ // mov derecha
			if(pos.getX()<(game.getWidth()-25))
			for(int i=1;i<=vel;i++){
				int v = pos.x + 1;
				pos.setLocation(v, pos.y);
				}
		}
		if(right && up && !down && !left){ // mov diagonal arrivaderecha
			if((pos.getX()<(game.getWidth()-25)) && (pos.getY()>0)){
			for(int i=1;i<=vel;i++){
				int v = pos.x + 1;
				pos.setLocation(v, pos.y);
				}
			for(int i=1;i<=vel;i++){
				int v = pos.y - 1;
				pos.setLocation(pos.x, v);
				}
			}
			
		}
		if(left && up && !right && !down){ //mov diagonal izquierdaarriva
			if((pos.getX()>0) && (pos.getY()>0)){
			for(int i=1;i<=vel;i++){			
				int v = pos.x - 1;
				pos.setLocation(v, pos.y);
				}
			for(int i=1;i<=vel;i++){
				int v = pos.y - 1;
				pos.setLocation(pos.x, v);
				}
			}
		}
		if(right && !up && down && !left){ // mov derechaabajo
			if((pos.getX()<(game.getWidth()-25))&&(pos.getY()<(game.getHeight()-80))){
			for(int i=1;i<=vel;i++){
				int v = pos.x + 1;
				pos.setLocation(v, pos.y);
				}
		
			for(int i=1;i<=vel;i++){
				int v = pos.y + 1;
				pos.setLocation(pos.x, v);
				}
			}
		}
		if(left && !up && !right && down){ // mov izquierda abajo
			if((pos.getX()>0)&&(pos.getY()<(game.getHeight()-80))){
			for(int i=1;i<=vel;i++){			
				int v = pos.x - 1;
				pos.setLocation(v, pos.y);
				}
			for(int i=1;i<=vel;i++){
				int v = pos.y + 1;
				pos.setLocation(pos.x, v);
				}
			}
		}
		this.setLocation(pos);
		
		game.getContentPane().repaint();
		if(shoot)
			this.shoot();
	}
	public void run(){ 
		up=false;
		down=false;
		left=false;
		right=false;
		shoot=false;
		listener1 listenerdi=new listener1(this);
		game.addKeyListener(listenerdi);
		listener2 listeneraa=new listener2(this);
		game.addKeyListener(listeneraa);
		listener3 listenerdisparo=new listener3(this);
		game.addKeyListener(listenerdisparo);
		while(this.isalive()){
			
			this.mover();
			try {
				Thread.sleep(25);
			} catch (InterruptedException e) {}
			
			}
		
		}

	protected class animacionexpjugador extends Thread{ //hago un thread para la explosion y cuando termina la exp lo hago null
		private JLabel a;
		public animacionexpjugador(JLabel l){a=l;}
		public void run(){
			a.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/explosion.gif")));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {}
			a.setIcon(null);
			game.youloose();
			
		}
	}
}
